<template>
  <div class="list">
    <!-- 顶部 -->
    <header v-show="isShow">
      <div class="returns">
        <i class="iconfont icon-fanhui" ></i>
      </div>
      <div class="search">
        <i class="iconfont icon-fangdajing"></i>
        <span>搜您喜欢的...</span>
      </div>
      <div class="go-home">
        <img src="@/assets/images/home.png" alt="" />
      </div>
    </header>

    <!-- 身体 -->
    <section>
      <!-- 左侧 -->
      <div class="list-l" ref="left">
        <ul class="l-item">
          <li :class='{active:i==currentIndex}' v-for="(item, i) in leftData" :key="i" @click="goScroll(i)">
            {{ item.name }}
          </li>
        </ul>
      </div>

      <!-- 右侧 -->
      <div class="list-r" ref="right">
        <div >
          <ul v-for="(item, i) in rightData" :key="i" class='shop-list'>
            <li class="shop" v-for="(out, o) in item" :key="o">
              <h2>{{ out.name }}</h2>
              <ul class="r-content">
                <li v-for="(main, m) in out.list" :key="m">
                  <img :src="main.imgUrl" alt="" />
                  <span>{{ main.name }}</span>
                </li>
              </ul>
            </li>
          </ul>
          <div >暂无更多数据</div>
        </div>
      </div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Tabbar from '@/components/common/Tabbar.vue';
import http from '@/common/api/request.js'
import BetterScroll from 'better-scroll'
export default {
  components: { Tabbar },
  name: "ListView",
  data() {
    return {
      isShow:true,       //顶部的显示与隐藏
      leftData: [],       //左侧数据
      rightData: [],      //右侧数据
      rightBScroll: '',   //右侧滑动BScroll
      allHeight: [],      //承载右侧每一块的高度值
      scrollY: ''         //右侧滚动距离
    }
  },
  computed:{
    // 监听右侧滑动距离
    currentIndex(){
      return this.allHeight.findIndex((item,index)=>{
        return this.scrollY>=item&&this.scrollY<this.allHeight[index+1]
      })
    }
  },
  methods:{
    // 滑动到第几项
    goScroll(index){
      this.rightBScroll.scrollTo(0,-this.allHeight[index],300)
    },
  },
  async created() {
    // 请求数据
    let res = await http.$axios({
      url: '/api/goods/list',
    })
    let left = []
    let right = []
    res.forEach((v, i) => {
      left.push({
        id: v.id,
        name: v.name
      })
      right.push(v.data)
    })
    this.leftData = left
    this.rightData = right

    // 添加滑动组件
    this.$nextTick(() => {
      // 左侧滑动
      new BetterScroll(this.$refs.left,{
        click:true
      })
      // 右侧滑动
      this.rightBScroll  = new BetterScroll(this.$refs.right,{
        click:true,
        probeType:3,
        bounce:false
      })
      // 统计右侧所有板块高度，并放入数组中
      let height=0
      this.allHeight.push(height)
      // 后去右侧每一块高度
      let uls=this.$refs.right.getElementsByClassName('shop-list')
      //把dom对象转换成功真正的数组
      Array.from(uls).forEach((v,i)=>{
        height+=v.clientHeight
        this.allHeight.push(height)
      })
      //得到右侧滚动的值
      this.rightBScroll.on('scroll',(pos)=>{
        this.scrollY = Math.abs( pos.y );
        if(this.scrollY>50){
          this.isShow=false
        }else{
          this.isShow=true
        }
      })
    })
  },
};
</script>

<style scoped lang="scss">
.list {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 1.173333rem;
  background-color: #b0352f;
  .returns {
    line-height: 1.173333rem;
    padding: 0 0.533333rem;
    i {
      color: #fff;
      font-size: 0.693333rem;
    }
  }
  .search {
    display: flex;
    align-items: center;
    flex: 1;
    padding: 0.16rem 0.266666rem;
    background-color: #ffffff;
    border-radius: 0.64rem;
    i {
      padding-right: 0.16rem;
      color: #666;
      font-size: 0.48rem;
    }
    span {
      color: #666;
      font-size: 0.373333rem;
    }
  }
  .go-home {
    padding: 0 0.266666rem;
    line-height: 1.173333rem;
    img {
      width: 0.48rem;
      height: 0.48rem;
    }
  }
}

section {
  display: flex;
  flex: 1;
  overflow: hidden;
}
.list-l {
  width: 2.48rem;
  background-color: #fff;
  border-right: 1px solid #cccccc;
  overflow: hidden;
  .l-item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    li {
      width: 100%;
      padding: 0.08rem 0;
      margin: 0.233333rem 0;
      text-align: center;
      font-size: 0.373333rem;
      &.active {
        color: #b54f4a;
        border-left: 6px solid #b54f4a;
      }
    }
  }
}

.list-r {
  flex: 1;
  overflow: hidden;
  div{
    div{
      width:100%;
      text-align:center;
      margin:7.3rem 0;
    }
  }
  .shop {
    text-align: center;
    h2 {
      padding: 0.533333rem 0;
      font-size: 0.64rem;
      font-weight: 400;
    }
    .r-content {
      display: flex;
      flex-wrap: wrap;
      li {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 33.33%;
        padding: 0.266666rem 0;
        img {
          width: 1.413333rem;
          height: 1.413333rem;
        }
        span {
          font-size: 0.426666rem;
        }
      }
    }
  }
}
::v-deep.tabbar {
  border-top: 1px solid #cccccc;
}
</style>
